{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-01-07T01:45:38.847543Z",
     "start_time": "2025-01-07T01:45:38.313904Z"
    }
   },
   "source": [
    "#12 数组中nan和inf\n",
    "import numpy as np\n",
    "a = np.nan\n",
    "b = np.inf\n",
    "print(a,type(a))\n",
    "print(b,type(b))\n",
    "# --判断数组中为 nan 的个数\n",
    "t = np.arange(24,dtype=float).reshape(4,6)\n",
    "# 将三行四列的数改成 nan\n",
    "t[3,4] = np.nan\n",
    "# 可以使用 np.count_nonzero() 来判断非零的个数\n",
    "print(np.count_nonzero(t))\n",
    "# 并 且 np.nan != np.nan 结果 是 TRUE\n",
    "# 所以我们可以使用这两个结合使用判断 nan 的个数\n",
    "print(np.count_nonzero(t != t))\n",
    "# 将 nan 替换为 0\n",
    "t[np.isnan(t)] = 0\n",
    "print(t)"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "nan <class 'float'>\n",
      "inf <class 'float'>\n",
      "23\n",
      "1\n",
      "[[ 0.  1.  2.  3.  4.  5.]\n",
      " [ 6.  7.  8.  9. 10. 11.]\n",
      " [12. 13. 14. 15. 16. 17.]\n",
      " [18. 19. 20. 21.  0. 23.]]\n"
     ]
    }
   ],
   "execution_count": 1
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T01:46:59.840978Z",
     "start_time": "2025-01-07T01:46:59.833497Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# ----------练习： 处理数组中 nan\n",
    "t = np.arange(24).reshape(4,6).astype('float')\n",
    "#将数组中的一部分替换 nan\n",
    "t[1,3:] = np.nan\n",
    "print(t)\n",
    "print('-------------------')\n",
    "# 遍历每一列， 然后判断每一列是否有 nan\n",
    "for i in range(t.shape[1]):\n",
    "    #获取当前列数据\n",
    "    temp_col = t[:,i]\n",
    "    # 判断当前列的数据中是否含有 nan\n",
    "    nan_num = np.count_nonzero(temp_col != temp_col)\n",
    "    # 条件成立说明含有 nan\n",
    "    if nan_num != 0:\n",
    "        # 将这一列不为 nan 的数据拿出来\n",
    "        temp_col_not_nan = temp_col[temp_col == temp_col]\n",
    "        # 将 nan 替换成这一列的平均值\n",
    "        temp_col[np.isnan(temp_col)] = np.mean(temp_col_not_nan)\n",
    "print(t)"
   ],
   "id": "72d6ddc18d440e00",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.  1.  2.  3.  4.  5.]\n",
      " [ 6.  7.  8. nan nan nan]\n",
      " [12. 13. 14. 15. 16. 17.]\n",
      " [18. 19. 20. 21. 22. 23.]]\n",
      "-------------------\n",
      "[[ 0.  1.  2.  3.  4.  5.]\n",
      " [ 6.  7.  8. 13. 14. 15.]\n",
      " [12. 13. 14. 15. 16. 17.]\n",
      " [18. 19. 20. 21. 22. 23.]]\n"
     ]
    }
   ],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T01:59:47.681111Z",
     "start_time": "2025-01-07T01:59:47.675253Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#13 二维数组的转置，轴滚动\n",
    "#对换数组的维度\n",
    "a = np.arange(12).reshape(3,4)\n",
    "print ('原数组： ')\n",
    "print (a )\n",
    "print ('\\n')\n",
    "print ('对换数组： ')\n",
    "print (np.transpose(a))"
   ],
   "id": "a7bdcb58f7108533",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原数组： \n",
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n",
      "\n",
      "\n",
      "对换数组： \n",
      "[[ 0  4  8]\n",
      " [ 1  5  9]\n",
      " [ 2  6 10]\n",
      " [ 3  7 11]]\n"
     ]
    }
   ],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T02:00:04.217495Z",
     "start_time": "2025-01-07T02:00:04.212143Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 与 transpose 一致\n",
    "a = np.arange(12).reshape(3,4)\n",
    "print ('原数组： ')\n",
    "print (a)\n",
    "print ('\\n')\n",
    "print ('转置数组： ')\n",
    "print (a.T)"
   ],
   "id": "897c40694e996b5c",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原数组： \n",
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n",
      "\n",
      "\n",
      "转置数组： \n",
      "[[ 0  4  8]\n",
      " [ 1  5  9]\n",
      " [ 2  6 10]\n",
      " [ 3  7 11]]\n"
     ]
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T02:00:19.919690Z",
     "start_time": "2025-01-07T02:00:19.913689Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 函数用于交换数组的两个轴\n",
    "t1 = np.arange(24).reshape(4,6)\n",
    "re = t1.swapaxes(1,0)\n",
    "print (' 原 数 组 ： ')\n",
    "print (t1)\n",
    "print ('\\n')\n",
    "print ('调用 swapaxes 函数后的数组： ')\n",
    "print (re)"
   ],
   "id": "c67eba0a768cbe01",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " 原 数 组 ： \n",
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "\n",
      "\n",
      "调用 swapaxes 函数后的数组： \n",
      "[[ 0  6 12 18]\n",
      " [ 1  7 13 19]\n",
      " [ 2  8 14 20]\n",
      " [ 3  9 15 21]\n",
      " [ 4 10 16 22]\n",
      " [ 5 11 17 23]]\n"
     ]
    }
   ],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T02:25:17.636389Z",
     "start_time": "2025-01-07T02:25:17.620257Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#轴滚动\n",
    "t=np.ones((3,4,5,6))\n",
    "t=np.rollaxis(t,3,2)\n",
    "print(t.shape)"
   ],
   "id": "cb38fd3c4ca99c67",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(3, 4, 6, 5)\n"
     ]
    }
   ],
   "execution_count": 10
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "",
   "id": "2c3c2bad6412d2aa"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
